package com.fenxiangle.yueding.feature.mine.view;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.FoucsBo;
import com.fenxiangle.yueding.framework.api.UserApi;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.widget.PowerfulEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class EditNickActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_edit_nick)
    PowerfulEditText mEdtEditNick;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_edit_nick;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        mToolbar.inflateMenu(R.menu.menu_complete);
        initToolBar(mToolbar, "昵称", true, true);
        mToolbar.setOnMenuItemClickListener(this);
    }

    @Override
    protected void initData() {
        String name = this.getIntent().getStringExtra("name");
        mEdtEditNick.setText(TextUtils.isEmpty(name)?"":name);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        editNick(mEdtEditNick.getText().toString());
        return false;
    }

    private void editNick(String nick) {
        if (TextUtils.isEmpty(nick)) {
            showMsg("请输入昵称！");
            return;
        }
        FoucsBo foucsBo = new FoucsBo();
        foucsBo.setUserName(nick);
        AM.api().createApiService(UserApi.class)
                .editNick(foucsBo)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        showLoading(d);
                    }

                    @Override
                    public void onNext(String s) {
                        dismissLoading();
                        showMsg("修改成功！");
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissLoading();
                        showMsg(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });

    }


}
